package com.gitee.wsl.doc.prosemirror.ui.ext

import com.gitee.wsl.doc.prosemirror.state.EditorState
import com.gitee.wsl.doc.prosemirror.ui.EditorView

sealed interface TextblockDir{
    enum class DirVertical:TextblockDir{
        up , down
    }
    enum class DirHorizontal:TextblockDir{
        left , right , forward , backward
    }
}

var cachedState: EditorState? = null
var cachedDir: TextblockDir ? = null
var cachedResult:Boolean = false
fun EditorView.endOfTextblock(state: EditorState, dir: TextblockDir):Boolean {
    val view = this
    if (cachedState == state && cachedDir == dir) return cachedResult
    cachedState = state; cachedDir = dir
    cachedResult = when(dir) {
        is TextblockDir.DirVertical -> endOfTextblockVertical(view, state, dir)
        is TextblockDir.DirHorizontal -> endOfTextblockHorizontal(view, state, dir)
    }
    return cachedResult
}

fun endOfTextblockVertical(view: EditorView, state: EditorState, dir: TextblockDir.DirVertical /*"up" | "down"*/):Boolean{
   TODO()
}

fun endOfTextblockHorizontal(view: EditorView, state: EditorState, dir: TextblockDir.DirHorizontal /*"left" | "right" | "forward" | "backward"*/):Boolean{
   TODO()
}


// Whether vertical position motion in a given direction
// from a position would leave a text block.
//function endOfTextblockVertical(view: EditorView, state: EditorState, dir: "up" | "down") {
//    let sel = state.selection
//            let $pos = dir == "up" ? sel.$from : sel.$to
//    return withFlushedState(view, state, () => {
//        let {node: dom} = view.docView.domFromPos($pos.pos, dir == "up" ? -1 : 1)
//        for (;;) {
//            let nearest = view.docView.nearestDesc(dom, true)
//            if (!nearest) break
//            if (nearest.node.isBlock) { dom = nearest.contentDOM || nearest.dom; break }
//            dom = nearest.dom.parentNode!
//        }
//        let coords = coordsAtPos(view, $pos.pos, 1)
//        for (let child = dom.firstChild; child; child = child.nextSibling) {
//        let boxes
//                if (child.nodeType == 1) boxes = (child as HTMLElement).getClientRects()
//                else if (child.nodeType == 3) boxes = textRange(child as Text, 0, child.nodeValue!.length).getClientRects()
//                else continue
//        for (let i = 0; i < boxes.length; i++) {
//        let box = boxes[i]
//        if (box.bottom > box.top + 1 &&
//            (dir == "up" ? coords.top - box.top > (box.bottom - coords.top) * 2
//        : box.bottom - coords.bottom > (coords.bottom - box.top) * 2))
//        return false
//    }
//    }
//        return true
//    })
//}


// Whether vertical position motion in a given direction
// from a position would leave a text block.
//function endOfTextblockVertical(view: EditorView, state: EditorState, dir: "up" | "down") {
//    let sel = state.selection
//            let $pos = dir == "up" ? sel.$from : sel.$to
//    return withFlushedState(view, state, () => {
//        let {node: dom} = view.docView.domFromPos($pos.pos, dir == "up" ? -1 : 1)
//        for (;;) {
//            let nearest = view.docView.nearestDesc(dom, true)
//            if (!nearest) break
//            if (nearest.node.isBlock) { dom = nearest.contentDOM || nearest.dom; break }
//            dom = nearest.dom.parentNode!
//        }
//        let coords = coordsAtPos(view, $pos.pos, 1)
//        for (let child = dom.firstChild; child; child = child.nextSibling) {
//        let boxes
//                if (child.nodeType == 1) boxes = (child as HTMLElement).getClientRects()
//                else if (child.nodeType == 3) boxes = textRange(child as Text, 0, child.nodeValue!.length).getClientRects()
//                else continue
//        for (let i = 0; i < boxes.length; i++) {
//        let box = boxes[i]
//        if (box.bottom > box.top + 1 &&
//            (dir == "up" ? coords.top - box.top > (box.bottom - coords.top) * 2
//        : box.bottom - coords.bottom > (coords.bottom - box.top) * 2))
//        return false
//    }
//    }
//        return true
//    })
//}